﻿using PPIDesktopClient.Entities;
using PPIDesktopClient.General;
using PPIDesktopClient.Logic;
using System;
using System.Data;
using System.Windows.Forms;
using VMind.Global;

namespace PPIDesktopClient
{
    public partial class frmPolygonEditor : frmDialogBase
    {
        #region Private Members
        private Int32 m_PolygonId = Int32.MinValue;
        private EUtility_Polygon m_SavedPolygon;
        private int m_ParentPolygon = Int32.MinValue;
        
        #endregion

        #region Public Properties
        public Int32 PolygonId
        {
            get { return m_PolygonId; }
            set { m_PolygonId = value; }
        }
        public int ParentPolygonId
        {
            get { return m_ParentPolygon; }
            set { m_ParentPolygon = value; }
        }
        #endregion

        public frmPolygonEditor()
        {
            InitializeComponent();
            txtFont.Font = dlgFont.Font;
            txtFont.Text = dlgFont.Font.Name + ", " + dlgFont.Font.Size.ToString() + "pt";
        }

        private void frmPolygonEditor_Load(object sender, EventArgs e)
        {
            LoadCombos();
            if (m_PolygonId != Int32.MinValue)
                LoadData();
            if (m_ParentPolygon != Int32.MinValue)
            {
                ddlParent.SelectedIndex = PPIDesktopClient.General.UIHelper.GetItemIndex(ddlParent, m_ParentPolygon.ToString());
            }
        }

        private void LoadCombos()
        {
            DataTable _Dt = LUtility_Polygon_PolygonType.SelectAllIdName();
            PPIDesktopClient.General.UIHelper.FillDropDown(ddlType, _Dt, "Name", "Utility_Polygon_PolygonTypeId", "Select an option...");
            _Dt = LUtility_Polygon.SelectAllStaticIdName();
            PPIDesktopClient.General.UIHelper.FillDropDown(ddlParent, _Dt, "Name", "Utility_PolygonId", "Select an option...");
        }

        private void LoadData()
        {
            VAL_ddlParent.CompareValidator = true;
            VAL_ddlParent.ValueToCompare = m_PolygonId.ToString();
            VAL_ddlParent.DataType = ValidationDataTypes.Integer;
            VAL_ddlParent.Operator = ValidationOperators.Different;
            EUtility_Polygon _Polygon = LUtility_Polygon.SelectCRUD(m_PolygonId);
            txtName.Text = _Polygon.Name;
            ddlType.SelectedIndex = PPIDesktopClient.General.UIHelper.GetItemIndex(ddlType, _Polygon.Utility_Polygon_PolygonType.Utility_Polygon_PolygonTypeId.ToString());
            if (_Polygon.Utility_Polygon_ParentPolygon.Utility_PolygonId != Int32.MinValue)
                ddlParent.SelectedIndex = PPIDesktopClient.General.UIHelper.GetItemIndex(ddlParent, _Polygon.Utility_Polygon_ParentPolygon.Utility_PolygonId.ToString());
            else
                ddlParent.SelectedIndex = 0;
            sldAltitude.Value = ConvertHelper.ToInt32(_Polygon.Altitude * 100);
            //FIXME: see if there's a better way to do this
            cpbLine.SelectedColor = System.Drawing.Color.FromArgb((_Polygon.LineColor - _Polygon.LineColor % 1000000) / 1000000, (_Polygon.LineColor % 1000000 - _Polygon.LineColor % 1000) / 1000, _Polygon.LineColor % 1000);
            sldWidth.Value = _Polygon.LineWidth;
            cpbFill.SelectedColor = System.Drawing.Color.FromArgb((_Polygon.FillColor - _Polygon.FillColor % 1000000) / 1000000, (_Polygon.FillColor % 1000000 - _Polygon.FillColor % 1000) / 1000, _Polygon.FillColor % 1000);
            sldOpacity.Value = _Polygon.FillOpacity;
            cpbFont.SelectedColor = System.Drawing.Color.FromArgb((_Polygon.FontColor - _Polygon.FontColor % 1000000) / 1000000, (_Polygon.FontColor % 1000000 - _Polygon.FontColor % 1000) / 1000, _Polygon.FontColor % 1000);
            dlgFont.Font = new System.Drawing.Font(_Polygon.Font, (float)8.25);
            txtFont.Font = dlgFont.Font;
            txtFont.Text = dlgFont.Font.Name + ", " + dlgFont.Font.Size.ToString() + "pt";
            DataTable _Dt = LUtility_Polygon_Point.SelectAllByPolygon(m_PolygonId);
            foreach (DataRow _Dr in _Dt.Rows)
                lstPoints.Items.Add(ConvertHelper.Todouble(_Dr["Latitude"]).ToString() + "; " + ConvertHelper.Todouble(_Dr["Longitude"]).ToString());
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            VAL_txtLatitude.Enabled = true;
            VAL_txtLongitude.Enabled = true;
            VAL_txtLatitude.Validate();
            VAL_txtLongitude.Validate();
            if (VAL_txtLatitude.IsValid && VAL_txtLongitude.IsValid)
                lstPoints.Items.Add(ConvertHelper.Todouble(txtLatitude.Text).ToString() + "; " + ConvertHelper.Todouble(txtLongitude.Text).ToString());
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstPoints.SelectedIndex > -1)
                lstPoints.Items.RemoveAt(lstPoints.SelectedIndex);
            btnRemove.Enabled = lstPoints.SelectedIndex > -1;
        }

        private void lstPoint_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRemove.Enabled = lstPoints.SelectedIndex > -1;
        }

        private void sldAltitude_ValueChanged(object sender, EventArgs e)
        {
            lblAltitude.Text = (Convert.ToDouble(sldAltitude.Value) / 100).ToString("F2");
        }

        private void btnFont_Click(object sender, EventArgs e)
        {
            dlgFont.ShowDialog();
            txtFont.Font = dlgFont.Font;
            txtFont.Text = dlgFont.Font.Name + ", " + dlgFont.Font.Size.ToString() + "pt";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            VAL_txtLatitude.Enabled = false;
            VAL_txtLongitude.Enabled = false;
            VAL_txtLatitude.Visible = false;
            VAL_txtLongitude.Visible = false;
            RFV_lstPoints.Visible = lstPoints.Items.Count < 3;
            if (ValidateAll() && !RFV_lstPoints.Visible)
            {
                EUtility_Polygon _Polygon = new EUtility_Polygon();
                _Polygon.Utility_PolygonId = m_PolygonId;
                _Polygon.Name = txtName.Text;
                _Polygon.Utility_Polygon_PolygonType = new EUtility_Polygon_PolygonType(Convert.ToInt32(((ListBoxItem)ddlType.SelectedItem).Value));
                if (ddlParent.SelectedIndex > 0)
                    _Polygon.Utility_Polygon_ParentPolygon = new EUtility_Polygon(Convert.ToInt32(((ListBoxItem)ddlParent.SelectedItem).Value));
                else
                    _Polygon.Utility_Polygon_ParentPolygon = new EUtility_Polygon();
                _Polygon.Altitude = Convert.ToDouble(sldAltitude.Value) / 100;
                //FIXME: see if there's a better way to do this
                _Polygon.LineColor = cpbLine.SelectedColor.R * 1000000 + cpbLine.SelectedColor.G * 1000 + cpbLine.SelectedColor.B;
                _Polygon.LineWidth = sldWidth.Value;
                _Polygon.FillColor = cpbFill.SelectedColor.R * 1000000 + cpbFill.SelectedColor.G * 1000 + cpbFill.SelectedColor.B;
                _Polygon.FillOpacity = sldOpacity.Value;
                _Polygon.FontColor = cpbFont.SelectedColor.R * 1000000 + cpbFont.SelectedColor.G * 1000 + cpbFont.SelectedColor.B;
                _Polygon.Font = dlgFont.Font.Name;
                _Polygon.Utility_Polygon_Points = new EUtility_Polygon_Point[lstPoints.Items.Count];
                double _latitude;
                double _longitude;
                string _coordinate;
                for (Int32 _I = 0; _I < lstPoints.Items.Count; _I++)
                {
                    _coordinate = lstPoints.Items[_I].ToString();
                    _latitude = ConvertHelper.Todouble(_coordinate.Substring(0, _coordinate.IndexOf(';')));
                    _longitude = ConvertHelper.Todouble(_coordinate.Substring(_coordinate.IndexOf(';') + 2, _coordinate.Length - _coordinate.IndexOf(';') - 2));
                    _Polygon.Utility_Polygon_Points[_I] = new EUtility_Polygon_Point(Int32.MinValue, new EUtility_Polygon(_Polygon.Utility_PolygonId), _latitude, _longitude);
                }
                m_SavedPolygon = _Polygon;
                SavePolygonProcess _Process = new SavePolygonProcess(_Polygon);

                _Process.SaveCompleted += new EventHandler(_Process_SaveCompleted);
                _Process.StartProcess();
                //LUtility_Polygon.Save(_Polygon);
                
            }
        }

        void _Process_SaveCompleted(object sender, EventArgs e)
        {
            m_ParentPolygon = Int32.MinValue;
            MessageBox.Show("The Polygon " + m_SavedPolygon.Utility_PolygonId.ToString() + " has been saved succesfully");
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
